<?php

namespace common\utils;

class MoneyUtil
{

    public static $map = [
        'DEM' => '德国马克',
        'EUR' => '欧元',
        'HKD' => '港元',
        'RMB' => '人民币',
        'USD' => '美元',
    ];

    /**
     * 金额的转换 京》兆》亿》万》元
     * @param string $money
     * @param int $accuracy
     * @return string|string
     */
    public static function format(string $money, int $accuracy = 2, string $append = '人民币')
    {
        $temp = str_replace(['人民币', '万', '元', '美元'], '', $money);
        $unit = strpos($money, '元')!==false?'元':'';
        if ($temp==0) {
            return '';
        }
        if (!is_numeric($temp)) {
            return $money;
        }
        if (($res = $temp/100000000)>=1) {
            return sprintf("%.".$accuracy."f", $res).'兆'.$unit.$append;
        }
        if (($res=$temp/10000)>=1) {
            return sprintf("%.".$accuracy."f", $res).'亿'.$unit.$append;
        } else {
            return sprintf("%.".$accuracy."f", $temp).'万'.$unit.$append;
        }
    }

    public static function showFormat($regCapital, $regCapitalUnit)
    {
        $regCapitalUnit = strtoupper($regCapitalUnit);
        $append = isset(self::$map[$regCapitalUnit]) ? self::$map[$regCapitalUnit] : '';
        return self::format($regCapital, 2, $append);
    }
}